package osSRC.schedule;

import osSRC.Format;

import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Scanner;


public class Main {
	static int choice;
	private final static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {
		choice = Format.selectMenu(new String[]{
				"1.round robin",
				"2.dynamic priority"
		}, scanner);
		LinkedList<PCB> linkedList = linkedList();
		if (linkedList.isEmpty()) {
			System.out.println("No programme set");
			return;
		}
		ProcessScheduler processScheduler;
		switch (choice) {
			case 1:
				processScheduler = new RoundRobinScheduler(linkedList, 2);
				break;
			case 2:
				processScheduler = new DynamicPriorityScheduler(linkedList);
				break;
			default:
				System.out.println("welcome to use again");
				return;
		}
		processScheduler.scheduling();
	}

	static int getRequiredTime() {
		int a, res;
		while (true) {
			try {
				a = scanner.nextInt();
				res = Math.abs(a);
				return res;
			} catch (InputMismatchException ime) {
				System.out.println();
				System.out.println("Please enter a NUMBER as the time");
				System.out.println("Press ENTER to try again(just enter the time)");
				scanner.next();
			}
		}
	}

	static LinkedList<PCB> linkedList() {
		String processName;
		LinkedList<PCB> res = new LinkedList<>();
		int priority = 0;
		int needTime;

		System.out.println("Please enter the name of process and time it needs");
		System.out.println("Or simply enter q to finish input");

		while (true) {
			processName = scanner.next();
			if (processName.equals("q")) break;
			if (res.size() >= 5) break;
			needTime = getRequiredTime();
			switch (choice) {
				case 1:
					res.add(new RoundRobinPCB(processName, ++priority, needTime));
					break;
				case 2:
					res.add(new DynamicPriorityPCB(processName, needTime));
					break;
				default:
					return null;
			}
		}
		return res;
	}
}